package com.tianyang.yukuaifu.rxservice.client;

import android.text.TextUtils;

import androidx.core.util.Pair;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.tianyang.yukuaifu.business.login.bean.LoginOutput;
import com.tianyang.yukuaifu.business.main.bean.DeviceOutput;
import com.tianyang.yukuaifu.business.main.bean.UserStoreOutput;
import com.tianyang.yukuaifu.business.trade.bean.SettleBankOutput;
import com.tianyang.yukuaifu.common.interfaces.UrlConstants;
import com.tianyang.yukuaifu.common.utils.CommonViewUtils;
import com.tianyang.yukuaifu.rxservice.BaseClient;
import com.tianyang.yukuaifu.rxservice.MetaBean;
import com.tianyang.yukuaifu.rxservice.interfaces.PlateUserService;

import java.util.List;
import java.util.Map;

import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.Observable;
import rx.functions.Func1;

/**
 * Description：plate-user接口
 * Created by xyj on 2024/3/18
 */
public class PlateUserClient extends BaseClient {

    private static PlateUserClient mInstance;

    public static PlateUserClient getInstance() {
        if (mInstance == null) {
            synchronized (PlateUserClient.class) {
                if (mInstance == null) {
                    mInstance = new PlateUserClient();
                }
            }
        }
        return mInstance;
    }

    private PlateUserService service;

    private PlateUserClient() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(UrlConstants.BASE_USER_URL)
                .client(client)
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        service = retrofit.create(PlateUserService.class);
    }

    /**
     * 修改密码
     *
     * @param reqData 请求参数
     */
    public Observable<JSONObject> updatePassword(Map<String, String> reqData) {
        return callMap(service.updatePassword(CommonViewUtils.getAuthorization(), reqData))
                .flatMap(new Func1<Pair<MetaBean, JSONObject>, Observable<JSONObject>>() {
                    @Override
                    public Observable<JSONObject> call(Pair<MetaBean, JSONObject> pair) {
                        return toObservable(pair);
                    }
                });
    }

    /**
     * 交易记录
     *
     * @param reqData 请求参数
     */
    public Observable<JSONObject> merchantTradeDetail(Map<String, String> reqData) {
        return callMap(service.merchantTradeDetail(CommonViewUtils.getAuthorization(), reqData))
                .flatMap(new Func1<Pair<MetaBean, JSONObject>, Observable<JSONObject>>() {
                    @Override
                    public Observable<JSONObject> call(Pair<MetaBean, JSONObject> pair) {
                        return toObservable(pair);
                    }
                });
    }

    /**
     * 商户结算卡查询
     *
     * @param reqData 请求参数
     */
    public Observable<SettleBankOutput> queryUserSettlementCard(Map<String, String> reqData) {
        return callMap(service.queryUserSettlementCard(CommonViewUtils.getAuthorization(), reqData))
                .flatMap(new Func1<Pair<MetaBean, JSONObject>, Observable<SettleBankOutput>>() {
                    @Override
                    public Observable<SettleBankOutput> call(Pair<MetaBean, JSONObject> pair) {
                        if (pair == null) {
                            return toObservable(null);
                        }
                        SettleBankOutput output = null;
                        if (pair.second != null && !TextUtils.isEmpty(pair.second.toString())) {
                            output = JSON.parseObject(JSON.toJSONString(pair.second), SettleBankOutput.class);
                        }
                        return toObservable(new Pair<>(pair.first, output));
                    }
                });
    }

    /**
     * 商户结算卡更改
     *
     * @param reqData 请求参数
     */
    public Observable<JSONObject> modifyUserSettlementCard(Map<String, String> reqData) {
        return callMap(service.modifyUserSettlementCard(CommonViewUtils.getAuthorization(), reqData))
                .flatMap(new Func1<Pair<MetaBean, JSONObject>, Observable<JSONObject>>() {
                    @Override
                    public Observable<JSONObject> call(Pair<MetaBean, JSONObject> pair) {
                        return toObservable(pair);
                    }
                });
    }

    /**
     * 店铺门店列表
     *
     * @param reqData 请求参数
     */
    public Observable<List<UserStoreOutput>> userMerchantStoresList(Map<String, String> reqData) {
        return callMapList(service.userMerchantStoresList(CommonViewUtils.getAuthorization(), reqData))
                .flatMap(new Func1<Pair<MetaBean, Object>, Observable<List<UserStoreOutput>>>() {
                    @Override
                    public Observable<List<UserStoreOutput>> call(Pair<MetaBean, Object> pair) {
                        if (pair == null) {
                            return toObservable(null);
                        }
                        List<UserStoreOutput> output = null;
                        if (pair.second != null && !TextUtils.isEmpty(pair.second.toString())) {
                            output = JSON.parseObject(JSON.toJSONString(pair.second), new TypeReference<List<UserStoreOutput>>() {
                            });
                        }
                        return toObservable(new Pair<>(pair.first, output));
                    }
                });
    }

    /**
     * 切换店铺
     */
    public Observable<LoginOutput> getAnotherStoresToken(int memberId) {
        return callMap(service.getAnotherStoresToken(
                UrlConstants.getAnotherStoresToken + "?memberId=" + memberId, CommonViewUtils.getAuthorization()))
                .flatMap(new Func1<Pair<MetaBean, JSONObject>, Observable<LoginOutput>>() {
                    @Override
                    public Observable<LoginOutput> call(Pair<MetaBean, JSONObject> pair) {
                        if (pair == null) {
                            return toObservable(null);
                        }
                        LoginOutput output = null;
                        if (pair.second != null && !TextUtils.isEmpty(pair.second.toString())) {
                            output = JSON.parseObject(JSON.toJSONString(pair.second), new TypeReference<LoginOutput>() {
                            });
                        }
                        return toObservable(new Pair<>(pair.first, output));
                    }
                });
    }

    /**
     * 绑定码牌
     */
    public Observable<JSONObject> confirmPosBind(Map<String, String> reqData) {
        return callMap(service.confirmPosBind(CommonViewUtils.getAuthorization(), reqData))
                .flatMap(new Func1<Pair<MetaBean, JSONObject>, Observable<JSONObject>>() {
                    @Override
                    public Observable<JSONObject> call(Pair<MetaBean, JSONObject> pair) {
                        return toObservable(pair);
                    }
                });
    }

    /**
     * 获取码牌
     */
    public Observable<JSONObject> getCompositePoster(Map<String, String> reqData) {
        return callMap(service.getCompositePoster(CommonViewUtils.getAuthorization(), reqData))
                .flatMap(new Func1<Pair<MetaBean, JSONObject>, Observable<JSONObject>>() {
                    @Override
                    public Observable<JSONObject> call(Pair<MetaBean, JSONObject> pair) {
                        return toObservable(pair);
                    }
                });
    }

    /**
     * 注销
     */
    public Observable<JSONObject> cancellationAccount() {
        return callMap(service.cancellationAccount(CommonViewUtils.getAuthorization()))
                .flatMap(new Func1<Pair<MetaBean, JSONObject>, Observable<JSONObject>>() {
                    @Override
                    public Observable<JSONObject> call(Pair<MetaBean, JSONObject> pair) {
                        return toObservable(pair);
                    }
                });
    }

    /**
     * 设备类型列表
     */
    public Observable<JSONObject> getDeviceTypeList() {
        return callMap(service.getDeviceTypeList(CommonViewUtils.getAuthorization()))
                .flatMap(new Func1<Pair<MetaBean, JSONObject>, Observable<JSONObject>>() {
                    @Override
                    public Observable<JSONObject> call(Pair<MetaBean, JSONObject> pair) {
                        return toObservable(pair);
                    }
                });
    }

    /**
     * 设备列表
     */
    public Observable<List<DeviceOutput>> getDeviceList(Map<String, String> reqData) {
        return callMapList(service.getDeviceList(CommonViewUtils.getAuthorization(), reqData))
                .flatMap(new Func1<Pair<MetaBean, Object>, Observable<List<DeviceOutput>>>() {
                    @Override
                    public Observable<List<DeviceOutput>> call(Pair<MetaBean, Object> pair) {
                        if (pair == null) {
                            return toObservable(null);
                        }
                        List<DeviceOutput> output = null;
                        if (pair.second != null && !TextUtils.isEmpty(pair.second.toString())) {
                            output = JSON.parseObject(JSON.toJSONString(pair.second), new TypeReference<List<DeviceOutput>>() {
                            });
                        }
                        return toObservable(new Pair<>(pair.first, output));
                    }
                });
    }

    /**
     * 绑定设备
     */
    public Observable<JSONObject> bindingDevice(Map<String, String> reqData) {
        return callMap(service.bindingDevice(CommonViewUtils.getAuthorization(), reqData))
                .flatMap(new Func1<Pair<MetaBean, JSONObject>, Observable<JSONObject>>() {
                    @Override
                    public Observable<JSONObject> call(Pair<MetaBean, JSONObject> pair) {
                        return toObservable(pair);
                    }
                });
    }

    /**
     * 修改设备名称
     */
    public Observable<JSONObject> modifyDeviceName(Map<String, String> reqData) {
        return callMap(service.modifyDeviceName(CommonViewUtils.getAuthorization(), reqData))
                .flatMap(new Func1<Pair<MetaBean, JSONObject>, Observable<JSONObject>>() {
                    @Override
                    public Observable<JSONObject> call(Pair<MetaBean, JSONObject> pair) {
                        return toObservable(pair);
                    }
                });
    }

    /**
     * 关联设备
     */
    public Observable<JSONObject> relationDevice(Map<String, String> reqData) {
        return callMap(service.relationDevice(CommonViewUtils.getAuthorization(), reqData))
                .flatMap(new Func1<Pair<MetaBean, JSONObject>, Observable<JSONObject>>() {
                    @Override
                    public Observable<JSONObject> call(Pair<MetaBean, JSONObject> pair) {
                        return toObservable(pair);
                    }
                });
    }

    /**
     * 取消关联设备
     */
    public Observable<JSONObject> unRelationDevice(Map<String, String> reqData) {
        return callMap(service.unRelationDevice(CommonViewUtils.getAuthorization(), reqData))
                .flatMap(new Func1<Pair<MetaBean, JSONObject>, Observable<JSONObject>>() {
                    @Override
                    public Observable<JSONObject> call(Pair<MetaBean, JSONObject> pair) {
                        return toObservable(pair);
                    }
                });
    }
}
